package com.atzxh.guigu.juc.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 小红同学
 * @create 2023-04-28-16:46
 * @description     演示线程池三种使用方式
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        // 一池5线程
        //ExecutorService threadPool1 = Executors.newFixedThreadPool(5);

        // 一池一线程
        //ExecutorService threadPool2 = Executors.newSingleThreadExecutor();

        // 可扩容线程
        ExecutorService threadPool3 = Executors.newCachedThreadPool();

        // 有10个顾客请求
        try {
            for (int i = 1; i <= 100; i++) {
                // 执行任务
                threadPool3.execute(()->{
                    System.out.println(Thread.currentThread().getName() + "  正在办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool3.shutdown();
        }
    }
}
